/*
 * Copyright (c) 2010 All Inclusive Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cares.fw.web.struts.taglib;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.commons.lang.StringUtils;

import com.cares.fw.coderesource.CodeManager;
import com.cares.fw.coderesource.CodeValue;
import com.cares.fw.coderesource.DefaultCodeManager;
import com.cares.fw.springframework.utils.SpringUtils;

public class LoadCodeTag extends SimpleTagSupport {
	
	/** No Selected */
	private static final String DEFAULT_EMPTY_LABEL = "-----";
	private static final String DEFAULT_EMPTY_VALUE = "";
	
	/** CODE ID*/
	private String id;
	
	/** Scope */
	private String scope;
	
	/** Attribute Name */
	private String name;
	
	/** Add No Selected or not*/
	private boolean empty;
	
	private String emptyValue = DEFAULT_EMPTY_VALUE;
	
	private String emptyLabel = DEFAULT_EMPTY_LABEL;
	
	/** Ignore if null */
	private boolean ignore = true;

	public boolean isIgnore() {
		return ignore;
	}

	public void setIgnore(boolean ignore) {
		this.ignore = ignore;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isEmpty() {
		return empty;
	}

	public void setEmpty(boolean empty) {
		this.empty = empty;
	}
	
	public String getEmptyValue() {
		return emptyValue;
	}

	public void setEmptyValue(String emptyValue) {
		this.emptyValue = emptyValue;
	}

	public String getEmptyLabel() {
		return emptyLabel;
	}

	public void setEmptyLabel(String emptyLabel) {
		this.emptyLabel = emptyLabel;
	}

	/**
	 * Process the tag.
	 */
	@Override
    public void doTag() throws JspException, IOException {
        if (StringUtils.isEmpty(id)) {
            throw new JspTagException("id is required.");
        }
        if (StringUtils.isEmpty(name)) {
            throw new JspTagException("name is required.");
        }
        
        // Get code resource
        CodeManager mgr = SpringUtils.getBean(CodeManager.class);
        if (mgr == null) {
        	mgr = new DefaultCodeManager();
        }
        List<CodeValue> codeList = mgr.getCodeValueList(id);
        if ((codeList == null || codeList.isEmpty()) && !ignore) {
        	throw new JspTagException("no resource found.[id=" + id + "]");
        }   
        
        List<CodeValue> options = new ArrayList<CodeValue>();
        if (empty) {
        	options.add(0, new CodeValue(id, emptyValue, emptyLabel));
        }
        options.addAll(codeList);

        if ("page".equals(scope)) {
            getJspContext().setAttribute(name, options, PageContext.PAGE_SCOPE);        	
        } else if ("request".equals(scope)) {
            getJspContext().setAttribute(name, options, PageContext.REQUEST_SCOPE);
        } else if ("session".equals(scope)) {
            getJspContext().setAttribute(name, options, PageContext.SESSION_SCOPE);
        } else if ("application".equals(scope)) {
            getJspContext().setAttribute(name, options, PageContext.APPLICATION_SCOPE);
        } else {
            getJspContext().setAttribute(name, options, PageContext.REQUEST_SCOPE);
        }
    }
}
